package com.zrrd.jt_admin.shop.goods.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zrrd.jt_admin.common.entity.*;
import com.zrrd.jt_admin.common.page.GoodsPageVo;
import com.zrrd.jt_admin.common.vo.GoodsVo;
import com.zrrd.jt_admin.shop.category.service.CategoryService;
import com.zrrd.jt_admin.shop.goods.mapper.GoodsMapper;
import com.zrrd.jt_admin.shop.goods.service.GoodsAttrsService;
import com.zrrd.jt_admin.shop.goods.service.GoodsBannerService;
import com.zrrd.jt_admin.shop.goods.service.GoodsService;
import com.zrrd.jt_admin.shop.goods.service.GoodsSkusService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wanjl
 * @since 2024-06-29
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Resource
    private CategoryService categoryService;
    @Resource
    private GoodsAttrsService goodsAttrsService;
    @Resource
    private GoodsBannerService goodsBannerService;
    @Resource
    private GoodsSkusService goodsSkusService;


    /**
     * 分页获取商品列表
     *
     * @param page
     * @param tab
     * @param title
     * @param categoryId
     * @return
     */
    @Override
    public GoodsPageVo findGoodsByPage(Integer page, Integer limit,String tab, String title, Integer categoryId) {
        PageHelper.startPage(page, limit);
        LambdaQueryChainWrapper<Goods> lambdaQuery = lambdaQuery();
        if (!title.equals("")) {
            lambdaQuery.like(Goods::getTitle, title);
        }
        if (categoryId!=null) {
            lambdaQuery.eq(Goods::getCategoryId, categoryId);
        }
        int isDel=0;
        switch (tab){
            case "all":
                break;
            case "checking":
                lambdaQuery.eq(Goods::getIscheck,0);
                break;
            case "not_passed":
                lambdaQuery.eq(Goods::getIscheck,2);
                break;
            case "saling":
                lambdaQuery.eq(Goods::getStatus,1);
                lambdaQuery.eq(Goods::getIscheck,1);
                break;
            case "off":
                lambdaQuery.eq(Goods::getStatus,0);
                break;
            case "min_stock":
                lambdaQuery.apply("stock<=min_stock");
                break;
            case "delete":
                isDel=1;
                break;
        }
        lambdaQuery.eq(Goods::getDel,isDel);
        List<Goods> list = lambdaQuery.orderByAsc(Goods::getId).list();
        PageInfo<Goods> pageInfo=new PageInfo<>(list);
        List<GoodsVo> goodsVoList = pageInfo.getList().stream().map(goods -> {
            GoodsVo goodsVo = new GoodsVo();
            BeanUtils.copyProperties(goods, goodsVo);
            Category category = categoryService.getById(goods.getCategoryId());
            goodsVo.setCategory(category);

            List<GoodsAttrs> goodsAttrsList = goodsAttrsService.lambdaQuery().eq(GoodsAttrs::getGoodsId, goods.getId()).list();
            goodsVo.setGoodsAttrs(goodsAttrsList);
            List<GoodsBanner> goodsBannerList = goodsBannerService.lambdaQuery().eq(GoodsBanner::getGoodsId, goods.getId()).list();
            goodsVo.setGoodsBanner(goodsBannerList);
            List<GoodsSkus> skusList = goodsSkusService.lambdaQuery().eq(GoodsSkus::getGoodsId, goods.getId()).list();
            goodsVo.setGoodsSkus(skusList);
            return goodsVo;
        }).collect(Collectors.toList());
        GoodsPageVo goodsPageVo = new GoodsPageVo();
        goodsPageVo.setList(goodsVoList);

        List<Category> categoryList = categoryService.list();
        goodsPageVo.setCates(categoryList);

        goodsPageVo.setTotalCount(pageInfo.getTotal());

        return goodsPageVo;
    }
}
